﻿using System.Linq;
using UnityEngine;
using zframe;

/// <summary>
///  熟悉Linq的各种语法和函数
/// </summary>
public class LearnLinqScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        AggregateTest();
        AllTest();
        AnyTest();  
        AverageTest();  
        DistinctTest();
        UnionTest();
        GroupByTest();
    }

    /// <summary>
    /// 累加器/聚合 函数 通过累加器返回结果
    /// </summary>
    private void AggregateTest()
    {
        //计算总和
        int[] myNum = new int[] { 10,2,5,20 };
        int totalNum = myNum.Aggregate((a, b) =>
        {
            return a + b;   
        });
        ZLogger.Log("LearnLinqScript AggregateTest 计算总和 totalNum=" + totalNum);

        //例如返回数组中的最大值
        myNum = new int[] { 50, 10, 2, 100, 58, 11, 5, 7 };
        int maxNum = myNum.Aggregate(myNum[0], (a,b) => {
            return a>b ? a : b;
        });
        ZLogger.Log("LearnLinqScript AggregateTest 数组中的最大值 maxNum=" + maxNum);



    }

    //对列表每个元素进行条件判断 是否所有都满足
    private void AllTest()
    {
        int[] myNum = new int[] {50,40,15,17};
        bool flag = myNum.All(x => x%2 == 0);  //所有元素都满足才返回true
        ZLogger.Log("LearnLinqScript AllTest 是否所有都是偶数 flag="+ flag);

    }

    //判断是否有元素满足条件
    private void AnyTest()
    {
        int[] myNum = new int[] { 50, 40, 15, 17 };
        bool flag = myNum.Any(x => x % 2 == 0);  //只要有一个满足条件则返回true
        ZLogger.Log("LearnLinqScript AllTest 是否有元素满足条件 偶数 flag=" + flag);
    }

    private void AverageTest()
    {
        int[] myNum = new int[] { 50, 40, 15, 17 };
        double averNum= myNum.Average();
        ZLogger.Log("LearnLinqScript AverageTest 计算平均值 偶数 flag=" + averNum);
    }

    //去重函数
    private void DistinctTest()
    {
        string[] myStrArr = new string[] { "a", "b", "c", "a", "b", "c" };
        
        myStrArr = myStrArr.Distinct().ToArray(); //去掉重复的元素
        ZLogger.Log("LearnLinqScript myStrArr=" + string.Join(",", myStrArr));
        
    }

    //去掉重复元素 也就是生成并集
    private void UnionTest()
    {
        string[] myStrArr1 = new string[] { "a", "b", "c", "d", "e",  };
        string[] myStrArr2 = new string[] { "a", "b", "c", "f", "g",  };
        string[] myStrArr3 = myStrArr1.Union(myStrArr2).ToArray();
        ZLogger.Log("LearnLinqScript UnionTest=" + string.Join(",", myStrArr3));

    }

    /// <summary>
    /// 分组函数
    /// </summary>
    private void GroupByTest()
    {
        int[] myNum = new int[] { 5,50, 40, 15, 17,1,2 };
        var g = myNum.GroupBy((a) => a%2==0); //根据条件分组，例如基数和偶数分开
        var gArr = g.ToArray();
        var evenG = gArr[0].ToArray();  
        ZLogger.Log("LearnLinqScript GroupByTest=" + gArr.Length);

        //根据学生相同的姓分组
        string[] myStrArr = new string[] {"赵11","钱11","孙11","李11" , "赵22", "钱22", "孙22", "李22" };
        var gName = myStrArr.GroupBy((a) => a[0],(a)=>a);
        var nameArr = gName.ToArray();  
        ZLogger.Log("LearnLinqScript GroupByTest="+ nameArr.Length); 

    }



}
